package com.example;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyActivity extends ListActivity{
    private RssListAdapter adapter;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         openNewsDialog();
    }

    private void openNewsDialog() {
        new AlertDialog.Builder(this)
                .setTitle(R.string.title_news)
                .setItems(R.array.news_paper,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialoginterface,
                                                int i) {
                                openNews(i);
                            }
                        })
                .show();
    }

    private void openNews(int i) {
        Log.d("JAJAL", "clicked on " + i);
//        TextView view = (TextView) findViewById(R.string.app_name);

        String URLNews = null;
        switch (i){
            case 0:
//                view.setText(R.string.google_news);
                URLNews = "http://news.google.com/?output=rss" ;
                break;
            case 1:
//                view.setText(R.string.google_news);
                URLNews = "http://indopos.co.id/index.php?option=com_ninjarsssyndicator&feed_id=2&format=raw" ;
                break;
            case 2:
//                view.setText(R.string.republikas_news);
                URLNews = "http://www.republika.co.id/rss" ;
                break;
            case 3:
//                view.setText(R.string.detik_news);
                URLNews = "http://rss.detik.com/" ;
                break;
            case 4:
                URLNews = "http://id.berita.yahoo.com/rss/nasional";
                break;
            case 5:
                URLNews = "http://unikmaya.blogspot.com/feeds/posts/default";
                break;

        }

        loadNews(URLNews);
    }

    private void loadNews(String urlNews) {
        List<JSONObject> jobs = new ArrayList<JSONObject>();
        try {
            jobs = RssReader.getLatestRssFeed(urlNews);
        } catch (Exception e) {
            Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
        }

        adapter = new RssListAdapter(this, jobs);

        setListAdapter(adapter);

        ListView lv = getListView();
        final List<JSONObject> finalJobs = jobs;
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
//                JSONObject object = null;
//                JSONObject req = new JSONObject((Map) finalJobs.get(position));
//                try {
//                    object = req.getJSONObject("link");
//                } catch (JSONException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
                try {
//                    Log.d("RSS CEK", "position : " + finalJobs.get(position).get("link"));
                    Object url = finalJobs.get(position).get("link");
                    Log.d("RSS CEK", "URL : " + url.toString());
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()));
                    startActivity(browserIntent);
                } catch (JSONException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
//                Log.d("RSS CEK", "position : "+position+"LONG : "+id+" JOBS : "+ object.toString());
              // When clicked, show a toast with the TextView text



            }
          });

    }
}
